<?php
namespace Home\Controller;
use Think\Controller;
use Org\Util\Step;
use Think\page;
class ShowController extends Controller {
	public function index(){
    	import("Org.Util.Step");
    	$id = $_GET['id'];  //要显示的文章id(如推荐，最新博文id)
    	$where = array('id' => $id ,'del' => 0);
    	//分类列表    	 
    	$all = M('step')->order('sort')->select();
    	$step = Step::showfenlei($all);    	
    	$this->assign('step',$step);  
    	//主体博文
    	$boke = D('bokegl')->relation(true)->where(array('del' =>0))->select();
    	$this->assign('boke',$boke);    
    	//推荐博文
    	$recommend = D('bokegl')->relation(true)->where(array('del' =>0 , 'attr[id]' =>(array('IN',2))))->limit(10)->select();
    	$this->assign('recommend',$recommend);
    	//最新发布博文
    	$new = M('boke')->order('time desc')->where(array('del' =>0))->limit(8)->select();    	
    	$this->assign('new',$new);
    	//正文部分显示
    	$show = M('boke');    	
    	$text = $show->where($where)->find();
    	$this->assign('text',$text);
    	//位置    	
    	$seat = Step::getparents($all, $text['cid']);    	
    	$this->assign('seat',$seat);    
    	//留言    	
    	$m = M('words');	
    	import('Think.Page');  // 导入分页类    	
    	$count = $m->where(array('pid' => $id))->count();  // 查询满足要求的总记录数
    	$this->assign('count',$count);  //用来判断是否有留言
    	$page  = new page($count,1);  //实例化分页类 传入总记录数和每页显示的记录数    	
    	$show = $page->show(); // 分页显示输出
    	$this->assign('page',$show);// 赋值分页输出    	
    	$words = $m->where(array('pid' => $id))->limit($page->firstRow.','.$page->listRows)->select();    	
    	$this->assign('words',$words);    	
    	$this->display();    	
    }
    public function serach(){
    	$name = $_POST['name'];
    	$where = (
    			array('name'=>$name)
    			);
    	$count = M('step')->where($where)->count();
    	if(!$count)
    	{$this->error("没有该列表！");}
    	else{
    		$arr = M('step')->where($where)->find();
    		$id = $arr['id'];
    		$this->redirect('Home/List/index/btid/'.$id);
    	}
    }    
    public function addwords(){
    	$data = array(
    		'user' => "游客".substr(time(), -2).rand(1,9999),
    		'content' => $_POST['content'],
    		'pid' => $_POST['id'],
    		'time' => time()	
    	);    	
    	$sum = M('words')->add($data);
    	if($sum){
    		$this->success('留言成功',U('Show/index',array('id'=>$_POST['id'])));
    	}else{
    		$this->error('留言失败',U('Show/index',array('id'=>$_POST['id'])));
    	}       	
    }    
}